package cn.rht.study.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.xmlbeans.impl.xb.xsdschema.impl.SchemaDocumentImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.rht.study.entity.User;
import cn.rht.study.mapper.UserMapper;
import cn.rht.study.service.IUserService;
import cn.rht.study.service.ex.EmptyListException;
import cn.rht.study.service.ex.OtherException;
import cn.rht.study.service.ex.PasswordNotMatchException;
import cn.rht.study.service.ex.UserNameIExitsException;
import cn.rht.study.service.ex.UsernameNotFoundException;

@Service
public class UserServiceImpl implements IUserService{
	
	private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	public UserMapper userMapper;
	
	@Override
	public void changeInfo(User user,String birthday) throws OtherException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date data = null;
		try {
			if(birthday!=null){
				data = sdf.parse(birthday);
				user.setBirthday(data);
			}
		}catch (ParseException e){
			log.error("时间格式转化异常");
			throw new OtherException("格式转换异常");
		}
		userMapper.changeInfo(user);
	}
	
	@Override
	public void createUser(User user)throws UserNameIExitsException,OtherException {
		User result = userMapper.findByUsername(user.getUsername());
		if(user.getId()!=null) {//不为Null为修改
			if(result!=null&&result.getId()!=user.getId()) {
				throw new UserNameIExitsException("用户名存在，重新修改");
			}
			Integer sum = userMapper.change(user);
//			System.err.println(sum);
			if(sum!=1) {
				throw new OtherException("发生异常，稍后再试");
			}
			log.info("用户信息修改成功");
			return;
		}
		if(result!=null) {
			throw new UserNameIExitsException("用户已存在");
		}
		Integer num = userMapper.add(user.getUsername(),user.getPassword());
//		System.err.println(num);
		if(num!=1) {
			throw new OtherException("发生异常，稍后再试");
		}
		log.info("用户添加信息成功");
	}

	@Override
	public void createPhone(User user) throws UserNameIExitsException, OtherException {
		User result = userMapper.findByPhone(user.getPhone());
		if(result.getId()!=null) {//不为Null为修改
			log.info("该手机号已存在");
			throw new OtherException("该手机号已存在");
		}
		user.setUsername(user.getPhone());
		Integer num = userMapper.addUser(user);
		if(num!=1) {
			throw new OtherException("发生异常，稍后再试");
		}
		log.info("用户添加信息成功");
	}

	@Override
	public User Login(String username,String pwd)throws UsernameNotFoundException,PasswordNotMatchException {
		User user = userMapper.findByUsername(username);
		if(user==null) {
			throw new UsernameNotFoundException("用户名不存在");
		}
		String password = user.getPassword();
		if(!password.equals(pwd)) {
			throw new PasswordNotMatchException("密码不正确");
		}
		log.info("本次返回的用户信息"+user);
		return user;
	}

	@Override
	public List<User> getUserList()throws UsernameNotFoundException {
		List<User> list = userMapper.findAll();
		if(list.size()==0) {
			throw new UsernameNotFoundException("用户不存在");
		}
		log.info("本次返回的所有用户列表"+list);
		return list;
	}

	@Override
	public void clean(Integer id) throws UsernameNotFoundException, OtherException {
		User user = userMapper.findById(id);
		if(user==null) {
			throw new UsernameNotFoundException("不存在该用户");
		}
		Integer row= userMapper.delete(id);
//		System.out.println(row);
		if(row!=1) {
			throw new OtherException("发生异常错误");
		}
		log.info("删除用户成功");
	}

	@Override
	public Integer createUsers(List<User> list) throws EmptyListException{
		if(list.size()==0) {
			throw new EmptyListException("导入的文件不能为空");
		}
		List<User> remove = new ArrayList<>();
		for(User user:list) {
			String name = user.getUsername();
			User result = userMapper.findByUsername(name);
			if(result!=null) {
				remove.add(user);
				log.info(name+"已存在，"+name+"添加失败!");
//				throw new UserNameIExitsException(name+"该用户已存在");
			}
		}
		list.removeAll(remove);
		if(list.size()==0) {
			throw new EmptyListException("导入失败，导入的用户都已存在");
		}
		Integer row = userMapper.addAll(list);
		log.info("导入"+row+"条数据成功");
		return row;
	}

	@Override
	public List<User> queryUser(String username) {
		List<User> list = userMapper.queryUserByName(username);
		log.info("本次返回的数据为={}",list);
		return list;
	}

	@Override
	public void uploadImg(String avatar,Integer id) {
		userMapper.uploadAvatar(avatar, id);
	}

	@Override
	public User findUserById(Integer id) {
		User user = userMapper.findById(id);
		return user;
	}

}
